<h1><%= t(".title") %></h1>

<p class="lead"><%= t(".description") %></p>

<%= paginate @models %>

<%= form_with url: update_models_path, method: :patch do |form| %>

  <table class="table table-striped" data-controller="bulk-edit" data-action="change->bulk-edit#handleCheckboxChange">
    <tr>
      <th><input type="checkbox" aria-label="<%= translate ".select_all" %>" value="0" name="bulk-select-all"></th>
      <th><%= Model.human_attribute_name(:name) %></th>
      <th><%= Model.human_attribute_name(:collection) %></th>
      <th><%= Model.human_attribute_name(:tags) %></th>
      <th><%= Model.human_attribute_name(:creator) %></th>
      <th><%= Model.human_attribute_name(:sensitive) %></th>
    </tr>
    <% @models.each do |model| %>
      <tr>
        <td><%= form.check_box "models[#{model.to_param}]", data: {bulk_item: model.to_param}, "aria-label": translate(".select", name: model.name) %></td>
        <td><%= link_to model.name, model, title: model.path %></td>
        <td><%= link_to model.collection.name, model.collection if model.collection %></td>
        <td><%= render "tag_list", tags: model.tags.order(taggings_count: :desc, name: :asc), model_id: model.to_param, show_count: false, filter: @filter %></td>
        <td><%= link_to model.creator.name, model.creator if model.creator %></td>
        <td><%= Icon(icon: "explicit", label: Model.human_attribute_name(:sensitive)) if model.sensitive %></td>
      </tr>
    <% end %>
  </table>

  <p class="lead"><%= t ".form_subtitle" %></p>

  <div class="row">
    <div class="col tabular-form">
      <%= render "bulk_fields", form: form %>

      <%= render "tags_edit", form: form, name: :add_tags, label: translate(".add_tags"), tags: @available_tags || [] %>
      <%= render "tags_edit", form: form, name: :remove_tags, label: translate(".remove_tags"), tags: [] %>

      <% if SiteSettings.show_libraries || current_user.is_administrator? %>
        <div>
          <%= form.label :new_library_id, class: "col-sm-2 col-form-label" %>
        </div>
        <div>
          <%= form.collection_select :new_library_id, policy_scope(Library).all, :id, :name, {include_blank: true}, {class: "form-control col-auto form-select"} %>
        </div>
      <% end %>

      <% if @filter.filtering_by?(:tag) %>
        <% @filter.parameter(:tag).each do |tag| %>
          <%= form.hidden_field :tag, multiple: true, value: tag %>
        <% end %>
      <% end %>
      <%= form.hidden_field :q, value: @filter.parameter(:q) if @filter.filtering_by?(:q) %>
      <%= form.hidden_field :collection, value: @filter.parameter(:collection) if @filter.filtering_by?(:collection) %>
      <%= form.hidden_field :library, value: @filter.parameter(:library) if @filter.filtering_by?(:library) %>
      <%= form.hidden_field :creator, value: @filter.parameter(:creator) if @filter.filtering_by?(:creator) %>
      <div>
        <%= form.submit translate(".submit"), class: "btn btn-primary" %>
        <%= form.submit translate(".update_all.label", count: @models.total_count), class: "btn btn-secondary", name: "update_all", data: {confirm: translate(".update_all.confirm", count: @models.total_count)} %>
        <%= form.submit translate(".merge"), class: "btn btn-outline-warning", formaction: merge_models_path, formmethod: :post, name: "merge" %>
      </div>
    </div>
    <% if !@filter.to_params.empty? %>
      <div class="col col-md-3">
        <%= render "filters_card" %>
      </div>
    <% end %>
  </div>

<% end %>
